﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Data;
//using System.Web.UI.WebControls;
using TTSBLL;
using TTS;
using TTSModel;

public partial class T0002368_Add : System.Web.UI.Page
{
    private Assemble a;
    private string message;
    private UnitType u;
    private const string kccDownPEC = "KCQR02AA";
    private IList<UnitTypeInfo> assembleVersionListT0002052;
    private IList<UnitTypeInfo> assembleVersionListT0002368;
    private string T0002052_SN { get { return ViewState["T0002052_SN"].ToString(); } set { ViewState["T0002052_SN"] = value; } }
    private string T0002368_SN { get { return ViewState["T0002368_SN"].ToString(); } set { ViewState["T0002368_SN"] = value; } }
    private string T0002368_KccUp_SN { get { return ViewState["T0002368_KccUp_SN"].ToString(); } set { ViewState["T0002368_KccUp_SN"] = value; } }
    private string T0002368_KccDown_SN { get { return ViewState["T0002368_KccDown_SN"].ToString(); } set { ViewState["T0002368_KccDown_SN"] = value; } }
   

    private DataRow dr;
    private DataTable dt
    {
        get
        {
            return (DataTable)ViewState["dt"];
        }
        set
        {
            ViewState["dt"] = value;
        }
    }
     public T0002368_Add()
    {
        a = new Assemble();
        u = new UnitType();
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            dt = new DataTable();
            CommonHelper.AssembleItemDataTableInitialize(dt);
            assembleVersionListT0002052 = u.LoadEntities("T0002052");
            assembleVersionListT0002368 = u.LoadEntities("T0002368");
            this.T0002052_Version.Text = assembleVersionListT0002052[0].TcodeVersion + "," + assembleVersionListT0002052[1].TcodeVersion;
            this.T0002368_Version.Text = assembleVersionListT0002368[0].TcodeVersion + "," + assembleVersionListT0002368[1].TcodeVersion;
            //this.T0002052_Version.Text = u.UnitType_GetbyTcode("T0002052").TcodeVersion;
            //this.T0002368_Version.Text = u.UnitType_GetbyTcode("T0002368").TcodeVersion;
        }
        this.TextBox_T0002052.Focus();
    }
    protected void TextBox_T0002052_TextChanged(object sender, EventArgs e)
    {
        T0002052_SN = this.TextBox_T0002052.Text.Trim();

        if (SNHelper.SNCheck(T0002052_SN, "T0002052", out message))
        {
            if (SNHelper.SNCheckVersionTwo(T0002052_SN, this.T0002052_Version.Text, out message))
            {
                if (ProConditionHelper.CheckLeafSubSNsPrecondition(T0002052_SN, "T0002052", out message))
                {
                    if (ProConditionHelper.CheckSubSNsPrecondition(T0002052_SN, "T0002052", out message))
                    {
                        this.TextBox_T0002052.Enabled = false;
                        this.TextBox_T0002368.Enabled = true;
                        this.TextBox_T0002368.Focus();
                        return;
                        //this.ConfirmSubmit.Enabled = false;
                    }
                }
            }
        }
        ClientScript.RegisterStartupScript(ClientScript.GetType(), "", "<script>alert('" + message + "');</script>");
        this.TextBox_T0002052.Text = "";
        this.TextBox_T0002052.Focus();
    }
    protected void TextBox_T0002368_TextChanged(object sender, EventArgs e)
    {
        T0002368_SN = this.TextBox_T0002368.Text.Trim();
        if (SNHelper.SNCheck(T0002368_SN, "T0002368", out message))
        {
            if (SNHelper.SNCheckVersionTwo(T0002368_SN, this.T0002368_Version.Text, out message))
            {
                if (ProConditionHelper.CheckMainSNsPrecondition(T0002368_SN, "T0002368", out message))
                {
                    this.TextBox_T0002368.Enabled = false;
                    /*this.ConfirmSubmit.Enabled = true;
                    this.ConfirmSubmit.Focus();*/
                    this.TextBox_T0002368_KccUp.Enabled = true;
                    this.TextBox_T0002368_KccUp.Focus();
                    return;
                    //this.ConfirmSubmit.Enabled = false;
                }
            }

        }
        ClientScript.RegisterStartupScript(ClientScript.GetType(), "", "<script>alert('" + message + "');</script>");
        this.TextBox_T0002368.Text = "";
        this.TextBox_T0002368.Focus();
    }
   protected void TextBox_T0002368_KccUp_TextChanged(object sender, EventArgs e)
    {
        T0002368_KccUp_SN = this.TextBox_T0002368_KccUp.Text.Trim();
        if (SNHelper.KccUpSNCheck(T0002368_KccUp_SN, out message))
        {
            if (ProConditionHelper.CheckLeafSubSNsPrecondition(T0002368_KccUp_SN, out message))
            {
                this.TextBox_T0002368_KccUp.Enabled = false;
                this.TextBox_T0002368_KccDown.Enabled = true;
                this.TextBox_T0002368_KccDown.Focus();
                return;
            }
        }
        // ClientScript.RegisterStartupScript(ClientScript.GetType(), "", "<script>alert('" + message + "');</script>");
        Page.ClientScript.RegisterStartupScript(this.GetType(), "y", "alert('" + message + "');", true);
        this.TextBox_T0002368_KccUp.Text = "";
        this.TextBox_T0002368_KccUp.Focus();
    }
    protected void TextBox_T0002368_KccDown_TextChanged(object sender, EventArgs e)
    {
        T0002368_KccDown_SN = this.TextBox_T0002368_KccDown.Text.Trim();

        if (SNHelper.KccDownSNCheck(T0002368_KccDown_SN, out message))
        {
            if (SNHelper.CheckKccDownPec(T0002368_KccDown_SN, kccDownPEC, out message))
            {
                if (SNHelper.SNCheckKccDownVersion(T0002368_KccDown_SN, "01", out message))
                {
                    this.TextBox_T0002368_KccDown.Enabled = false;
                    this.ConfirmSubmit.Enabled = true;
                    this.ConfirmSubmit.Focus();
                    return;
                }
            }
        }
        ClientScript.RegisterStartupScript(ClientScript.GetType(), "", "<script>alert('" + message + "');</script>");
        this.TextBox_T0002368_KccDown.Text = "";
        this.TextBox_T0002368_KccDown.Focus();
    }
    protected void ConfirmSubmit_Click(object sender, EventArgs e)
    {
        if (ProConditionHelper.CheckMainSNsPrecondition(T0002368_SN, "T0002368", out message))
        {
            if (ProConditionHelper.CheckLeafSubSNsPrecondition(T0002368_KccUp_SN, out message))
            {
                if (ProConditionHelper.CheckLeafSubSNsPrecondition(T0002052_SN, "T0002052", out message))
                {
                   
                        Unit u = new Unit();
                        u.Insert(new UnitInfo() { SN = T0002052_SN, Tcode = "T0002052", IsLeaf = true, IsRoot = true });
                        u.Insert(new UnitInfo() { SN = T0002368_SN, Tcode = "T0002368", IsLeaf = true, IsRoot = true });
                        // u.Insert(new UnitInfo() { SN = T0002368_KccUp_SN, Tcode = "T0002368", IsLeaf = true, IsRoot = true });
                        // u.Insert(new UnitInfo() { SN = T0002368_KccDown_SN, Tcode = "T0002368", IsLeaf = true, IsRoot = true });
                        KCCSNUnion ku = new KCCSNUnion();
                        KCCSNUnionInfo kui = new KCCSNUnionInfo() { KCCSN1 = T0002368_KccUp_SN, KCCSN2 = T0002368_KccDown_SN, SN = T0002368_SN, UserID = "111113" };
                        ku.Insert(kui);

                        AssembleInfo ainfo = new AssembleInfo()
                        {
                            ID = 1,
                            SN = T0002368_SN,
                            IsRoot = true,
                            RecordTime = DateTime.Now,
                            Tcode = "T0002368",
                            Userid = "111116"
                        };
                        dt.Rows.Clear();
                        dr = dt.NewRow();
                        dr["SubSN"] = T0002052_SN;
                        dr["Remark"] = "T0002052_BackplaneAssembly";
                        dt.Rows.Add(dr);

                        /*  dr = dt.NewRow();
                          dr["SubSN"] = T0002368_KccUp_SN;
                          dr["Remark"] = "DM_T0002368_KccUp";
                          dt.Rows.Add(dr);

                          dr = dt.NewRow();
                          dr["SubSN"] = T0002368_KccDown_SN;
                          dr["Remark"] = "DM_T0002368_KccDown";
                          dt.Rows.Add(dr);*/


                        if (a.Assemble_Insert(ainfo, dt))
                        {
                            message = "录入成功";

                            this.TextBox_T0002368_KccUp.Text = "";
                            this.TextBox_T0002368_KccDown.Text = "";
                            this.TextBox_T0002368.Text = "";
                            this.TextBox_T0002052.Text = "";
                            this.TextBox_T0002052.Enabled = true;
                            this.TextBox_T0002052.Focus();
                            this.ConfirmSubmit.Enabled = false;

                        }
                        else
                        {
                            message = "录入失败，请联系相关人员";
                        }
      
                  
                }
            }
        } 
        ClientScript.RegisterStartupScript(ClientScript.GetType(), "", "<script>alert('" + message + "');</script>");
    }
    protected void Return_Click(object sender, EventArgs e)
    {
        Response.Redirect("DM.aspx");
    }

   
}